package com.aibili.contoller.administrator;

import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.aibili.contoller.BaseController;
import com.aibili.pojo.BannerInfo;
import com.aibili.pojo.EventInfo;
import com.aibili.pojo.EventType;
import com.aibili.pojo.EventUser;
import com.aibili.pojo.model.PageBodyModel;
import com.aibili.pojo.model.PageModel;
import com.aibili.service.EventService;
import com.aibili.service.FileService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.daoshun.common.CommonUtils;
import com.daoshun.common.DsConstant;

/**
 * 活动的控制器
 */
@Controller
@RequestMapping("/administrator")
public class EventController extends BaseController {

	@Resource
	private EventService eventService;

	@Resource
	private FileService fileService;

	/**
	 * 
	 * @Description banner 信息
	 * @param pageIndex
	 * @param pageSize
	 * @return
	 */
	@RequestMapping("/bannerinfo")
	public ModelAndView bannerinfo() {
		ModelAndView mav = new ModelAndView();

		List<BannerInfo> infos = eventService.getAllBanner();

		mav.addObject("list", infos);
		return mav;
	}

	/**
	 * 
	 * @Description banner 的新增和修改
	 * @param id
	 * @param url
	 * @param imgsfile
	 * @return
	 */
	@RequestMapping("/banneredit")
	public String banneredit(String id, String url, @RequestParam("imgsfile") MultipartFile imgsfile) {
		String filepath = "";
		if (!CommonUtils.isEmptyString(id)) { // 修改
			BannerInfo info = eventService.getBannerById(CommonUtils.parseLong(id, 0));
			if (imgsfile != null && !imgsfile.isEmpty()) {
				filepath = fileService.saveFile(imgsfile);
				info.setPic(filepath);
			}
			info.setUrl(url);
			eventService.updateBannerInfo(info);
		} else { // 新增

			List<BannerInfo> infos = eventService.getAllBanner();
			if (infos != null && infos.size() < 5) {
				BannerInfo info = new BannerInfo();
				if (imgsfile != null && !imgsfile.isEmpty()) {
					filepath = fileService.saveFile(imgsfile);
					info.setPic(filepath);
					info.setUrl(url);
					eventService.addBannerInfo(info);
				}
			}
		}

		return "redirect:/administrator/bannerinfo";
	}

	/**
	 * 
	 * @Description 通过 id 删除 banner
	 * @param id id
	 * @return String
	 */
	@ResponseBody
	@RequestMapping(value = "/delBanner", produces = "application/json; charset=UTF-8")
	public String delBanner(String id) {
		HashMap<String, Object> map = new HashMap<String, Object>();
		if (!CommonUtils.isEmptyString(id)) {
			eventService.deleteBannerById(CommonUtils.parseLong(id, 0));
		}
		map.put("code", 0);
		return JSON.toJSONString(map);
	}

	/**
	 * 类型列表
	 * 
	 * @param phone
	 * @param pageIndex
	 * @return ModelAndView
	 */
	@RequestMapping("/eventtypeinfo")
	public ModelAndView eventtypeinfo(String phone, Integer pageIndex) {
		ModelAndView mav = new ModelAndView();

		if (pageIndex == null || pageIndex <= 0) {
			pageIndex = 1;
		}

		PageBodyModel<EventType> results = eventService.getEventType(pageIndex, DsConstant.PAGESIZE);
		if (results != null) {
			List<EventType> types = results.getList();
			PageModel page = results.getPageModel();
			mav.addObject("list", types);
			mav.addObject("page", page);
		}
		return mav;
	}

	/**
	 * 
	 * @Description 获取所有的活动类别
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/alleventtype", produces = "application/json; charset=UTF-8")
	public String alleventtype(HttpServletRequest request) {
		HashMap<String, Object> map = new HashMap<String, Object>();

		List<EventType> list = eventService.getAllEventtype();
		map.put("list", list);

		return JSON.toJSONString(map);
	}

	/**
	 * 
	 * @Description 通过 id 删除活动类别
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/delete_eventtype", produces = "application/json; charset=UTF-8")
	public String deleteEventType(String id) {
		HashMap<String, Object> map = new HashMap<String, Object>();
		if (!CommonUtils.isEmptyString(id)) {
			eventService.deleteEventType(CommonUtils.parseLong(id, 0));
			map.put("code", 0);
		}

		return JSON.toJSONString(map);
	}

	/**
	 * 跳转到编辑类型页面
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("/editorEventType")
	public ModelAndView editorEventType(String id) {
		ModelAndView mov = new ModelAndView();
		if (!CommonUtils.isEmptyString(id)) {
			EventType info = eventService.getEventTypeBy(CommonUtils.parseLong(id, 0));
			if (!CommonUtils.isEmptyString(info.getTitle_json())) {
				String title_json = info.getTitle_json().substring(1, info.getTitle_json().length() - 1);
				title_json = title_json.replaceAll("\"", "");
				String[] title_list = title_json.split(",");
				mov.addObject("title_list", title_list);
			}
			if (!CommonUtils.isEmptyString(info.getIntro_json())) {
				String intro_json = info.getIntro_json().substring(1, info.getIntro_json().length() - 1);
				intro_json = intro_json.replaceAll("\"", "");
				String[] intro_list = intro_json.split(",");
				mov.addObject("intro_list", intro_list);
			}
			mov.addObject("info", info);
		}
		mov.setViewName("administrator/editorEventType");
		return mov;
	}

	/**
	 * 添加或编辑类型
	 * 
	 * @param id
	 * @param type_name
	 * @param title_json
	 * @param intro_json
	 * @return
	 */
	@RequestMapping("/eventTypeEditor")
	public ModelAndView updateEventType(String id, String type_name, String[] title_json, String[] intro_json) {
		ModelAndView mov = new ModelAndView();
		JSONArray titleArray = new JSONArray();
		if (title_json != null && title_json.length > 0) {
			for (String str : title_json) {
				str.replaceAll(" ", "");
				if (!CommonUtils.isEmptyString(str)) {
					titleArray.add(str);
				}
			}
		}

		JSONArray introArray = new JSONArray();
		if (intro_json != null && intro_json.length > 0) {
			for (String str : intro_json) {
				str.replaceAll(" ", "");
				if (!CommonUtils.isEmptyString(str)) {
					introArray.add(str);
				}
			}
		}

		if (!CommonUtils.isEmptyString(type_name)) {
			if (CommonUtils.isEmptyString(id)) {
				eventService.addEventType(type_name, titleArray.toString(), introArray.toString());
			} else {
				eventService.updateEventType(CommonUtils.parseLong(id, 0), type_name, titleArray.toString(),
						introArray.toString());
			}
		}
		mov.setViewName("redirect:/administrator/eventtypeinfo");
		return mov;
	}

	/**
	 * 
	 * @Description 活动消息
	 * @param pageIndex
	 * @param pageSize
	 * @param event_type
	 * @return
	 */
	@RequestMapping("/eventinfo")
	public ModelAndView eventinfo(Integer pageIndex, String event_type) {
		ModelAndView mav = new ModelAndView();

		if (pageIndex == null || pageIndex <= 0) {
			pageIndex = 1;
		}

		PageBodyModel<EventInfo> results = eventService.getByType(0, event_type, pageIndex, DsConstant.PAGESIZE);
		if (results != null) {
			List<EventInfo> users = results.getList();
			PageModel page = results.getPageModel();
			mav.addObject("list", users);
			mav.addObject("page", page);
		}
		mav.addObject("event_type", event_type);
		return mav;
	}

	/**
	 * 
	 * @Description 活动消息详情
	 * @param id
	 * @return
	 */
	@RequestMapping("/eventinfodetail")
	public ModelAndView eventinfodetail(String id) {
		ModelAndView mav = new ModelAndView();

		EventInfo info = eventService.getEventInfoById(CommonUtils.parseLong(id, 0));

		mav.addObject("obj", info);
		return mav;
	}

	/**
	 * 查看参加活动的人
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("/eventJoin")
	public ModelAndView eventJoin(String id, Integer pageIndex) {
		ModelAndView mov = new ModelAndView();
		if (pageIndex == null || pageIndex <= 0) {
			pageIndex = 1;
		}
		PageBodyModel<EventUser> results = eventService.getEventUserByEventPage(CommonUtils.parseLong(id, 0),
				pageIndex);
		if (results != null) {
			List<EventUser> list = results.getList();
			PageModel page = results.getPageModel();
			mov.addObject("list", list);
			mov.addObject("page", page);
		}
		mov.addObject("id", id);
		return mov;
	}

}
